/**
 * 
 */
package com.kexinde.wjifen.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.kexinde.wjifen.dao.impl.ShopDao;
import com.kexinde.wjifen.dao.impl.ShopkeeperDao;
import com.kexinde.wjifen.model.Shop;
import com.kexinde.wjifen.model.Shopkeeper;
import com.kexinde.wjifen.service.IShopkeeperManager;

/**
 * @author vincent
 * 
 */
@Component("shopkeeperManager")
public class ShopkeeperManager implements IShopkeeperManager {

	private ShopDao shopDao;
	private ShopkeeperDao shopkeeperDao;

	@Override
	public List<Shopkeeper> showAllShopkeepers(long shopId) {
		// TODO Auto-generated method stub
		Shop shop = this.shopDao.findById(shopId);
		return shop.getShopkeepers();
	}

	@Override
	public Shopkeeper showShopkeeper(String userName) {
		// TODO Auto-generated method stub
		return this.shopkeeperDao.findByName(userName);
	}

	@Override
	public boolean add(Shopkeeper shopkeeper) {
		// TODO Auto-generated method stub
		if (!this.exist(shopkeeper.getUserName()))
			return this.shopkeeperDao.doInsert(shopkeeper);
		else
			return false;
	}

	@Override
	public boolean delete(Shopkeeper shopkeeper) {
		// TODO Auto-generated method stub
		return this.shopkeeperDao.doDelete(shopkeeper);
	}

	@Override
	public boolean modifyTel(String shopkeeperUserName, String tel) {
		// TODO Auto-generated method stub
		Shopkeeper shopkeeperTemp = this.shopkeeperDao
				.findByName(shopkeeperUserName);
		shopkeeperTemp.setSelfPhone(tel);
		return this.shopkeeperDao.doUpdate(shopkeeperTemp);
	}

	@Override
	public boolean check(String userName, String password) {
		// TODO Auto-generated method stub
		boolean result = false;
		Shopkeeper shopkeeper = this.shopkeeperDao.findByName(userName);
		if (shopkeeper != null)
			if (password.equals(shopkeeper.getPassword()))
				result = true;
		return result;
	}

	@Override
	public boolean exist(String userName) {
		// TODO Auto-generated method stub
		boolean result = false;
		Shopkeeper shopkeeper = this.shopkeeperDao.findByName(userName);
		if (null != shopkeeper)
			result = true;
		return result;
	}

	public ShopDao getShopDao() {
		return shopDao;
	}

	@Resource
	public void setShopDao(ShopDao shopDao) {
		this.shopDao = shopDao;
	}

	public ShopkeeperDao getShopkeeperDao() {
		return shopkeeperDao;
	}

	@Resource
	public void setShopkeeperDao(ShopkeeperDao shopkeeperDao) {
		this.shopkeeperDao = shopkeeperDao;
	}

	@Override
	public boolean modifyPwd(String shopkeeperUserName, String newPwd,
			String oldPwd) {
		Shopkeeper shopkeeperTemp = this.shopkeeperDao
				.findByName(shopkeeperUserName);
		if (shopkeeperTemp.getPassword().equals(oldPwd)) {
			shopkeeperTemp.setPassword(newPwd);
			this.shopkeeperDao.doUpdate(shopkeeperTemp);
			return true;
		}
		return false;
	}

}